﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace desktop
{
    public static class Extension
    {
        /// <summary>
        /// 返回路径中的文件夹及文件名，带扩展名。
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetName(this string path)
        {
            try
            {
                int index = path.LastIndexOf('/');
                if (index == -1)
                {
                    index = path.LastIndexOf("\\");
                }

                return path.Substring(index + 1);
            }
            catch
            {
                return "";
            }

        }
        /// <summary>
        /// 获取扩展名
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetExtensionName(this string path)
        {
            try
            {
                return path.Substring(path.LastIndexOf(".") + 1);
            }
            catch
            {
                return "";
            }

        }
        /// <summary>
        /// 返回路径中的文件夹及文件名，不具备扩展名。
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetFileNameWithoutExtension(this string path)
        {
            try
            {
                string name = path.Substring(path.LastIndexOf("\\") + 1);
                if (name.LastIndexOf(".") == -1)
                    return name;
                int length = name.Length - name.Substring(name.LastIndexOf(".")).Length;
                return name.Substring(0, length);
            }
            catch
            {
                return "";
            }
        }


    }
}
